#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    int longestConsecutive(vector<int> &nums)
    {
        unordered_map<int, bool> m;
        for (auto num : nums)
        {
            m.insert({num, true});
        }
        int ret = 0;
        for (auto x : m)
        {
            if (x.second == true)
            {
                int len = 0;
                x.second = false;
                int more = x.first + 1;
                int less = x.first - 1;
                while (m.count(more))
                {
                    len++;
                    m[more] = false;
                    more++;
                }
                while (m.count(less)){
                    len++;
                    m[less] = false;
                    less--;
                }
                ret = max(len, ret);
            }
        }
        return ret;
    }
};